/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function(candidates, target) {
  let res = []
  function dfs (path, index) {
    let total = path.reduce((total, cur) => total + cur, 0)
    if (total >= target) {
      if (total === target) res.push([...path]) 
      return
    }
    for (let i = index; i < candidates.length; i++) { 
      path.push(candidates[i])
      dfs(path, i)
      path.pop()
    }
  }
  dfs([], 0)
  return res
};

console.log(combinationSum([2,3,6,7], 7));